#include <stdlib.h>
#include <stdio.h>
#include "../include/interrupt.h"

Interrupt *interrupt_create()
{
	Interrupt *interrupt = (Interrupt *) malloc(sizeof(Interrupt));

	interrupt->number[0] = 0;
	interrupt->number[1] = 0;
	
	//semáforo semhi, compartilhado entre threads do processo e começando em 1
	sem_init(&(interrupt->semhi), 0, 1);

	printf("Interruption created.\n");
	return interrupt;
}


void set_interrupt(Interrupt *i, int n)
{ 
    if(n == MEMORY_INTERRUPT_NUMBER)
		i->number[0] = n;
    else
    {
    	sem_wait(&(i->semhi));
    	i->number[1] = n;
    }
     
    printf("\tINT %d\n", n);
}

		
int get_interrupt(Interrupt *i)
{ 
    if(i->number[0]>0)
    	return i->number[0];
    else
    	return i->number[1];
}

		
void reset_interrupt(Interrupt *i, int n)
{
    if (n == MEMORY_INTERRUPT_NUMBER)
      i->number[0] = 0;
    else
      {
	 i->number[1] = 0;
	 sem_post(&(i->semhi));
      }
    
}

